<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改【请填写功能名称】')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-sample-edit" th:object="${sample}">
        <input name="samId" th:field="*{samId}" type="hidden">
        <h4 class="form-header h4">服务选择</h4>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">请选择服务：</label>
            <div class="col-sm-8">
                <input type='text' readonly id='input-select-service' class='form-control select2-multiple' onclick='selectServices()' th:value="${service.serName}" required>
                <input type="hidden" name="serId" id="input-h-serId" th:value="${service.serId}">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">请选择方法：</label>
            <div class="col-sm-8">
                <input type='text' readonly id='input-select-method' class='form-control select2-multiple' onclick='selectMethod()' th:value="${method.modName}" required>
                <input type="hidden" name="modId" id="input-h-modId" th:value="${method.modId}">
            </div>
        </div>
        <h4 class="form-header h4">样品信息修改</h4>
        <div class="form-group">
            <label class="col-sm-3 control-label">名称：</label>
            <div class="col-sm-8">
                <input name="samName" th:field="*{samName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">Slot：</label>
            <div class="col-sm-8">
                <input name="samSlot" th:field="*{samSlot}" class="form-control" type="number" min="1" max="999" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input name="samDes" th:field="*{samDes}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">质量：</label>
            <div class="col-sm-8">
                <input name="quantity" th:field="*{quantity}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分子式：</label>
            <div class="col-sm-8">
                <input name="mFormula" th:field="*{mFormula}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">分子量：</label>
            <div class="col-sm-8">
                <input name="mWeight" th:field="*{mWeight}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">溶剂：</label>
            <div class="col-sm-8">
                <select class="form-control" id="solvent" name="solvent" required>
                    <option th:each="solvent:${method.solvents}" th:value="${solvent.solventName}" th:text="${solvent.solventName}" th:field="*{solvent}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">优先：</label>
            <div class="col-sm-8">
<!--                <input readonly name="priority" th:field="*{status}" class="form-control" type="text">-->
<!--                <input type="checkbox" id="priority" th:checked="${service.priority == '1' ? true : false}" >-->
                <label class="toggle-switch switch-solid">
                    <input type="checkbox" id="priority" th:checked="${service.priority == '1' ? true : false}">
                    <span></span>
                </label>
            </div>
        </div>
        <input type="hidden" name="infId" th:value="${service.infId}">
    </form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "system/sample";
    var prefix2 = ctx + "system/request2";
    var serId = [[${service.serId}]];

    $("#form-sample-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = $('#form-sample-edit').serializeArray();
            var status = $("input[id='priority']").is(':checked') == true ? 1 : 0;
            data.push({"name": "status", "value": status});
            $.operate.save(prefix2 + "/serEdit", data);
        }
    }

    function selectServices(){
        var options = {
            title: 'Select Service',
            url: prefix2 + "/selService",
            callBack: doSubmit
        };
        $.modal.openOptions(options);

        function doSubmit(index, layero){
            var rows = layero.find("iframe")[0].contentWindow.getSelections();
            var ids = layero.find("iframe")[0].contentWindow.getIdSelections();
            if (rows.length == 0) {
                $.modal.alertWarning("请选择一个服务");
                return;
            }
            $("#input-select-service").attr("value", rows);
            $("#input-h-serId").attr("value", ids)
            $("#input-select-method").attr("value", "");
            $("#input-h-modId").attr("value", "");
            $("#solvent").empty();

            $.modal.close(index);
        }
    }

    function selectMethod(){
        if(serId == null || serId < 1){
            $.modal.alertError("请先选择服务");
        }else {
            var options = {
                title: 'Select Method',
                url: prefix2 + "/selMethod?serId="+serId,
                callBack: doSubmit
            };
            $.modal.openOptions(options);

            function doSubmit(index, layero){
                var rows = layero.find("iframe")[0].contentWindow.getSelections();
                var ids = layero.find("iframe")[0].contentWindow.getIdSelections();
                if (rows.length == 0) {
                    $.modal.alertWarning("请选择一个方法");
                    return;
                }
                $("#input-select-method").attr("value", rows);
                $("#input-h-modId").attr("value", ids);
                console.log(ids);
                //solvent修改
                var config = {
                    url: prefix2 + "/selSolvents",
                    type: "post",
                    dataType: "json",
                    data: {"modId": ids.join("")},
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍候...");
                    },
                    success: function(result) {
                        //回调事件
                        $("#solvent").empty();
                        $.each(result, function(index, value){
                            $("#solvent").append('<option value="'+value.solventName+'">'+value.solventName+'</option>');
                        })
                        $.modal.closeLoading();
                        // $.operate.successTabCallback(result); 不需要刷新
                    }
                };
                $.ajax(config)
                $.modal.close(index);
            }
        }
    }
</script>
</body>
</html>